//
//  SecondTemplateVC.m
//  AStage_shougongke
//
//  Created by 赵磊 on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SecondTemplateVC.h"

@interface SecondTemplateVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSArray *dataArr;
// 标题中的名字 描述 图片
@property (nonatomic, retain) NSString *topic_name;
@property (nonatomic, retain) NSString *topic_des;
@property (nonatomic, retain) NSString *host_pic;
@end

@implementation SecondTemplateVC
// 页面即将出现 将tabBar显示
- (void)viewWillAppear:(BOOL)animated
{
    // tabBar显示
    self.tabBarController.tabBar.hidden = YES;
}
// 页面即将消失 将tabBar隐藏
- (void)viewWillDisappear:(BOOL)animated
{
    // tabBar隐藏
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.separatorStyle = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self dataHandler];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"cell"];
    [_tableView release];
    self.navigationItem.title = @"专题详情";
}
- (void)dataHandler
{
    [AFNModel GETWithURL:[NSString stringWithFormat:@"http://m.shougongke.com/index.php?c=Shiji&a=topicInfo&topic_id=%@&vid=15", self.topicId] completion:^(id result) {
        NSArray *arrTemp = result[@"data"][@"goods"];
        self.dataArr = [SecondModel modelHandlerWithArray:arrTemp];
        self.topic_name = result[@"data"][@"topic_name"];
        self.topic_des = result[@"data"][@"topic_des"];
        self.host_pic = result[@"data"][@"host_pic"];
        [self createHeader];
        [_tableView reloadData];
    }];
}
- (void)createHeader
{
    // header中描述的高度
    CGFloat h = [HeightAdaption heightWithText:self.topic_des Size:16 Width:WIDTH - 20];
    CGFloat heightOfTitle = [HeightAdaption heightWithText:self.topic_name Size:20 Width:WIDTH - 20];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH / 2 + h + 20)];
    UIImageView *headerImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, WIDTH / 2)];
    [headerImg sd_setImageWithURL:[NSURL URLWithString:self.host_pic]];
    headerImg.backgroundColor = [UIColor yellowColor];
    [headerView addSubview:headerImg];
    UILabel *headerTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH - 20, heightOfTitle)];
    headerTitle.font = [UIFont systemFontOfSize:20];
    headerTitle.center = headerImg.center;
    headerTitle.text = self.topic_name;
    headerTitle.textColor = [UIColor whiteColor];
    headerTitle.numberOfLines = 0;
    [headerImg addSubview:headerTitle];
    UILabel *headerDes = [[UILabel alloc] initWithFrame:CGRectMake(10, WIDTH / 2 + 10, WIDTH - 20, h)];
    headerDes.text = self.topic_des;
    headerDes.font = [UIFont systemFontOfSize:16];
    headerDes.numberOfLines = 0;
    [headerView addSubview:headerDes];
    _tableView.tableHeaderView = headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondModel *m = _dataArr[indexPath.row];
    CGFloat heightOfPic = [m.pic_height floatValue] / [m.pic_width floatValue] * (WIDTH - 20);
    CGFloat heightOfDes = [HeightAdaption heightWithText:m.des Size:15 Width:WIDTH - 20];
    return heightOfDes + heightOfPic + 90;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    SecondModel *m = _dataArr[indexPath.row];
    NSDictionary *dic = m.info;
    cell.scale = [m.pic_height floatValue] / [m.pic_width floatValue];
    cell.numberLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
    cell.nameLabel.text = m.title;
    cell.desLabel.text = m.des;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.pic]];
    cell.priceLabel.text = [NSString stringWithFormat:@"￥%@", dic[@"yh_price"]];
    cell.sumLabel.text = [NSString stringWithFormat:@"%.0f人已买", [dic[@"sum"] floatValue]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondModel *m = _dataArr[indexPath.row];
    NSDictionary *dic = m.info;
    WebViewController *webVC = [[WebViewController alloc] init];
    webVC.url = [@"http://h5.m.taobao.com/cm/snap/index.html?u_channel=1-23259936&umpChannel=1-23259936&exParams=%7B%22umpChannel%22:%221-23259936%22,%22u_channel%22:%221-23259936%22,%22referer%22:%22showTaoKeItemDetail%22%7D&ttid=2014_0_23259936@baichuan_ios_1.8.3&id=" stringByAppendingString:dic[@"num_iid"]];
    [self.navigationController pushViewController:webVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
